﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using DBHelper;
using Server;

namespace ZoneLine
{
    public class PlcAddrListViewModel : ViewModelBase
    {
        private t_PlcAddr _SelectedModel;
        private List<t_PlcAddr> _Models;
        private RelayCommand _DelCmd;
        private List<t_PlcAddr> _listAddr;
        private string _selectCon;

        public override bool Init()
        {
            //this.Models = DALContext.GetModels<t_PlcAddr>("1=1");
            ListAddr = new List<t_PlcAddr>();
            //ListAddr.AddRange(PlcHelper.GetPlcAddrs().OrderBy(p=>p.Address));
            Models = ListAddr;
            return true;
        }

        public List<t_PlcAddr> ListAddr { get => _listAddr; set => Set(ref _listAddr, value); }
        public t_PlcAddr SelectedModel { get => _SelectedModel; set => Set(ref _SelectedModel, value); }

        public List<t_PlcAddr> Models { get => _Models; private set => Set(ref _Models, value); }

        public RelayCommand DelCmd => (_DelCmd ?? (_DelCmd = new RelayCommand(Del, CanDel)));
        public string SelectCon
        {
            get => _selectCon;
            set => Set(ref _selectCon, value);
        }

        private async void Del()
        {
            if (SelectedModel == null)
            {
                return;
            }
            if (await Confirm($"确定删除编号为【{SelectedModel.NO}】的信息?"))
            {
                bool rlt = DbContext.DeleteModel<t_PlcAddr>($"NO = '{SelectedModel.NO}'");
                if (rlt)
                {
                    ShowMsg("删除成功！");
                }
                else
                {
                    ShowMsg("删除失败！");
                }
                Init();
            }
        }

        private bool CanDel()
        {

            return SelectedModel != null && SelectedModel.ID > 0;
        }

        #region 下发命令

        private RelayCommand sendCmd;
        public RelayCommand SendCmd => sendCmd ?? (sendCmd = new RelayCommand(Send, CanSend));

        private void Send()
        {
            try
            {
                //result = PlcHelper.SetTag(SelectedModel.NO, short.Parse(CmdNum)).IsSuccess;
              
            }
            catch (Exception ex)
            {
                ShowMsg("命令下发失败");
            }
        }

        private bool CanSend()
        {
            return SelectedModel?.ID != 0 && !string.IsNullOrEmpty(CmdNum) && IsCmdNum(CmdNum);
        }
        private static bool IsCmdNum(string code)
        {
            Regex regex = new Regex("^[0-99]*$");
            return regex.IsMatch(code);
        }

        private string _cmdNum;
        public string CmdNum
        {
            get => _cmdNum;
            set => Set(ref _cmdNum, value);
        }

        #endregion

        #region 更改使能

        private RelayCommand enableCmd;
        public RelayCommand EnableCmd => enableCmd ?? (enableCmd = new RelayCommand(Enable, CanEnable));

        public void Enable()
        {
          
        }
        private bool CanEnable()
        {
            return SelectedModel.ID > 0;
        }
        #endregion
        #region 根据条件筛选地址

        //private RelayCommand _selectCommand;
        //public RelayCommand SelectCmd => _selectCommand ?? (_selectCommand = new RelayCommand(Select,CanSelect));

        public void Select(string con)
        {
            ListAddr.Clear();
            if (con?.Length > 0)
            {
                Models = PlcHelper.GetPlcAddrs().Where(p => p.Name.Contains(con)).OrderBy(p => p.Name).ThenBy(p => p.Address).ToList();
            }
            else
            {
                Models = PlcHelper.GetPlcAddrs();
            }

        }

        private bool CanSelect()
        {
            return true;
        }

        #endregion

    }
}
